<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 17-5-15
 * Time: 下午7:56
 */

namespace common\components\tools;


use common\models\ImageIndex;
use common\models\MultiMediaUploadQuene;
use yii\base\ErrorException;
use yii\helpers\ArrayHelper;
use yii\helpers\FileHelper;

class Bank
{
    const DIR_CREDIT = 'credit';
    const DIR_SPECIAL_CARD = 'special-card';
    const DIR_NEW_CAR = 'new-car';
    const DIR_USED_CAR = 'used-car';
    const DIR_MANUAL_PICS = 'manual-pics';
    const DIR_MULTI_MEDIA = 'multi-media';

    /**
     * 提交图片
     * @param $dirname
     * @param $pics
     * @return mixed
     * @throws \yii\base\Exception
     */
    public static function UploadIcbcPicsCommon($dirname, $pics)
    {
        $dir_pre = '@uploads_root/images/'.$dirname;
        foreach ($pics as $k => $v){
            if (isset($v['picurl'])&&$v['picurl']){
                $m = ImageIndex::imageModelGet($v['picurl'], $dir_pre);
                $y = $m->target_path;
                $pics[$k]['picurl'] = \Yii::$app->params['pic_url_type'].str_replace('/', '_', \Yii::getAlias('@uploads_url/images/'.$dirname)."/".$y);
            }
        }
        return $pics;
    }
    
    /**
     * 提交征信图片资源
     * @param $pics
     * @return array
     * @throws
     */
    public static function uploadIcbcPics0($pics, $pass7 = 0, $text = '')
    {
        $docx_web = Word::createCreditWord($pics);
        $pics = ArrayHelper::map($pics, 'picid', 'picurl');
        $dir_pre = '@uploads_root/images/'.self::DIR_CREDIT;
        if ($pics['0004']){
            $a = ImageIndex::imageModelGet($pics['0004'], $dir_pre);
            if ($pass7){
                $_image = $a->imageFilePath;
                Image::addTextWater($_image, $_image, $text);
                $y = $a->target_path;
            }else{
                $y = $a->target_path;
            }
        }else{
            throw new ErrorException('图片上传pic错误', 10514);
        }
        $pics= [
            ['picid'=>'0005', 'picurl'=>\Yii::$app->params['pic_url_type'].str_replace('/', '_', $docx_web)],
            ['picid'=>'0004', 'picurl'=>\Yii::$app->params['pic_url_type'].str_replace('/', '_', \Yii::getAlias('@uploads_url/images/credit')."/".$y)],
        ];
        return $pics;
    }

    public static function uploadMultiMedia($dirname, $urls, $custom_id)
    {
        $dir_pre = \Yii::getAlias("@uploads_root/medias/{$dirname}"."/{$custom_id}");
//        $zip = basename($dir_pre).".zip";
        $zip_path = $dir_pre.".zip";
        if (is_dir($dir_pre)){
            FileHelper::removeDirectory($dir_pre, ['traverseSymlinks' => true]);
            FileHelper::createDirectory($dir_pre);
        }else{
            FileHelper::createDirectory($dir_pre);
        }
        if (file_exists($zip_path)){
            unlink($zip_path);
        }
        foreach ($urls as $k => $v){
            if (isset($v['picurl'])&&$v['picurl']){
                $y = basename($v['picurl']);
                if(strpos($y,'?') !== false){
                    if(strpos($y,'?name=') !== false){
                        $z = Tools::str2arr($y, '?name=');
                        $y = $z[1];
                    }else{
                        $y = substr($y, -36);
                    }
                }
                $x = file_get_contents($v['picurl']);
                $len =  strlen($x);  //获取文件大小
                $lenM = $len/(1024*1024);
                if ($lenM > 30){
                    return MultiMediaUploadQuene::STATUS_FILE_LARGE_30M;
                }
                $z = $dir_pre."/".$y;
                file_put_contents($z, $x);
            }
        }
//        $zip = basename($dir_pre).".zip";
        $zip_path = $dir_pre.".zip";
        $_zip_url = \Yii::$app->params['pic_url_type'].str_replace('/', '_', \Yii::getAlias("@uploads_url/medias/{$dirname}")."/{$custom_id}".".zip");
        Zip::makeDirToZip($dir_pre, $zip_path);
        FileHelper::removeDirectory($dir_pre, ['traverseSymlinks' => true]);
//        exit;
        return [
            [
                'picid' => '8888',
                'picurl' => $_zip_url,
            ],
        ];
    }
}